#!/bin/sh



#Usage: ./rpm_verification srcrpm_path

#docker内部的脚本
test_sh=test_in_docker.sh

#完成rpm的install、service、cmd、remove测试
test_in_docker()
{
	#搜索此目录下所以的rpm文件
	rpm_file=$(find $HOME -type f -name "*.rpm")
	#获取rpm包名
	rpm_name_version=${rpm_file##*/}
	echo -e "RPM: ${rpm_name_version}\n"
	
	rpm_name=$(echo "${rpm_name_version}" | sed -e s/-[[:digit:]]./@/|  awk -F '@' '{print $1}')
	installed_rpm_name=${rpm_name_version%.*}
	#echo "${installed_rpm_name}"
	
	#安装rpm之前，安装依赖
	#获取依赖列表
	requires_list=$(rpm -qRp $rpm_name_version | grep -v '(' |  grep -v '/')
	
	#判断是否存在安装依赖
	if [ "$requires_list" != "" ];then
		#去除版本号
		install_soft=$(echo $requires_list | sed 's/>= [0-9].[0-9]//g' )
		echo -e "install $installed_rpm_name need software lists:${install_soft}\n"
		#安装编译依赖
		for soft in $install_soft
		do	
			echo -e "install need software:$soft ....... \n"
			yum install -y $soft
			if [ $? -ne 0 ];then
				echo -e "yum install -y $soft failed !!!\n"
				exit 1	
			else
				echo -e "yum install -y $soft successed !!!\n"
			fi
		done
		echo -e "install $installed_rpm_name need software:${install_soft} successed !!!\n"
	fi
	
	install_test
	rpm_service_test
	rpm_cmd_help_test
	remove_test
}

#rpm包的安装测试
install_test()
{
	echo -e "=========================== Start ${rpm_name} Install Test ==========================="	
	#安装rpm包,已安装需卸载
	if [[ $(rpm -qa ${rpm_name}) = "${installed_rpm_name}" ]];then
		echo -e "${installed_rpm_name} has beend installed,now remove it\n"
		rpm -e ${installed_rpm_name}
	fi
	
	echo -e "start install test........."
	#执行安装
	rpm -ivh ${rpm_name_version} 
	#判断是否安装成功,失败则退出测试
	if [ $? -eq 0 -a $(rpm -qa ${rpm_name}) = "${installed_rpm_name}" ];then
		echo -e "Install Status: Success\n\n"
	else
		echo -e "Install Status: Fail\n\n"
		exit 1
	fi	
}


#rpm包中service测试,即service启停与状态
rpm_service_test()
{
	echo -e "=========================== Start ${rpm_name} Service Start and Stop Test ==========================="	
	
	#搜索rpm包中包含的service
	if [ $(rpm -ql ${rpm_name} | grep -E "\.service$" | awk -F "/" '{print $NF}' | wc -l) -ne 0 ];then
		rpm_services_list=$(rpm -ql ${rpm_name} | grep -E "\.service$" | awk -F "/" '{print $NF}')
		echo -e "${rpm_name} Service: ${rpm_services_list}\n"
	else
		echo -e "${rpm_name} Has no Service, So do not Service Start and Stop Test !!!\n"
		return 1
	fi
	
	#记录service的数量
	local service_num=0 
	
	#循环测试每个service的启停
	for service in ${rpm_services_list}
	do
		service_num=$(($service_num+1))
		echo -e "${service_num} ${service} start test.........\n"
		#启动service
		systemctl start ${service}
		if [ $? -eq 0 ];then
			echo -e "Start ${service} Status: Success\n"
			
			echo -e "{service_num} ${service} status test.........\n"
			#查看service状态
			systemctl status ${service}
			if [ $? -eq 0 ];then
				echo -e "Test ${service} Status: Success\n"
			else
				echo -e "Test ${service} Status: Fail\n"
				continue
			fi
			
			echo -e "{service_num} ${service} stop test.........\n"
			#停止service
			systemctl stop ${service}
			if [ $? -eq 0 ];then
				echo -e "Stop ${service} Status: Success\n"
			else
				echo -e "Stop ${service} Status: Fail\n"
				continue
			fi
		else
			echo -e "Start ${service} Status: Fail\n"
			continue
		fi	
	done
}

#rpm包中指令的--help测试,即兼容性测试
rpm_cmd_help_test()
{
	echo -e "=========================== Start ${rpm_name} CMD --help Test ==========================="
	#搜索rpm包中包含的指令
	if [ $(rpm -ql ${rpm_name} | grep -E '/s?bin/' | awk -F "/" '{print $NF}' | wc -l) -ne 0 ];then
			rpm_bin_list=$(rpm -ql ${rpm_name} | grep -E '/s?bin/')
			#echo -e "${rpm_name} CMD:\n${rpm_bin_list}\n"
	else
			echo -e "${rpm_name} Has no CMD, So do not CMD --help Test !!!\n"
			return 1
	fi
	
	#记录cmd的数量
	local cmd_num=0
	
	#循环测试每个cmd --help
	for bin_file in ${rpm_bin_list}
	do
	
		if [ -x ${bin_file} ];then
			cmd_num=$(($cmd_num+1))
			cmd=$(echo ${bin_file} | awk -F "/" '{print $NF}')
			echo -e "【${cmd_num}】 ${cmd} --help "
			echo -e "------------------------------------------------------------"
			#启动cmd -h
			${cmd} | grep -- --help >/dev/null 2>&1
			if [ $? -eq 0 ];then
				${cmd} --help 
				if [ $? -eq 0 -o $? -eq 64 ];then
					echo -e "------------------------------------------------------------"
					echo -e "CMD Status: Success\n\n"
				else
					echo -e "------------------------------------------------------------"
					echo -e "CMD Status: Fail\n\n"
					continue
				fi
			fi
		else
			continue
		fi
	done
	if [ ${cmd_num} -eq 0 ];then
		echo -e "${rpm_name} Has no CMD, So do not CMD --help Test !!!\n"
		return 1
	fi
}

#rpm包的卸载测试
remove_test()
{
	echo -e "=========================== Start ${rpm_name} Remove Test ==========================="	
	#执行卸载
	yum remove -y ${rpm_name}
	if [ $? -eq 0 ];then
		echo -e "Remove Status: Success\n\n"
	else
		echo -e "Remove Status: Fail\n\n"
		exit 1
	fi
}

test_in_docker
